package Persistencia;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Vector;

import Controlador.Sistema;
import Controlador.Utils;
import Negocio.ItemMineral;
import Negocio.LoteCompuesto;
import Negocio.LoteSimple;
import Negocio.Mineral;

public class AdmPersistenciaLoteSimple extends AdministradorPersistencia {

	private static AdmPersistenciaLoteSimple instancia;
	
	
	
	public static AdmPersistenciaLoteSimple getInstancia() {
		if (instancia == null)
			instancia = new AdmPersistenciaLoteSimple();
		return instancia;
	}

	public int generarCodigo(){
		try
		{
		int cant =0;
		Connection con = PoolConnection.getPoolConnection().getConnection();
		PreparedStatement s = con.prepareStatement("Select Count(*) from lote");
		ResultSet res=  s.executeQuery();
		while (res.next())
		{
			cant = res.getInt(1);
		}
		PoolConnection.getPoolConnection().realeaseConnection(con);
		return cant;
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
			
		}
		return 0;

	}


	public void delete(Object o) {
		System.out.println("Entro al delete");
		try
		{
			LoteSimple a = (LoteSimple)o;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("update  Lote set estado = ? where codigo = ?");
			s.setString(1,Utils.CONSTANTE_INACTIVO);
			s.setLong(2, a.getNro());
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		}
		catch (Exception e){
			System.out.println(e.getMessage());
		}
	}


	public void insert(Object o) {
		System.out.println("Entro al insert");
		try
		{
			LoteSimple a = (LoteSimple)o;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("insert into lote values (?,?,?,?,?,?)");
			//agregar campos
			s.setLong(1,a.getNro());
			s.setLong(2, a.getMineralPrincipal().getCodigo() );
			s.setString(3, Utils.CONSTANTE_LOTESIMPLE);
			s.setLong(4,0);
			s.setDouble(5,a.getAreaSiembra());
			s.setString(6, Utils.CONSTANTE_ACTIVO);
			
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		}
		catch (Exception e){
			System.out.println(e.getMessage());
		}
	}



	@Override
	public Vector<Object> select(Object o) {
		Vector <Object> dev = new Vector<Object>(); 
		try{
			LoteSimple a = (LoteSimple)o;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("select * from Lote where codigo = ?");
			s.setLong (1,a.getNro());
			ResultSet rta = s.executeQuery();
			if(rta.next()){
				Mineral m = new Mineral();
				m = Mineral.getInstancia().buscarMineral(rta.getLong(2));
				a.setMineralPrincipal(m);
				a.setLote_que_compone(rta.getLong(4));
				a.setAreaSiembra(rta.getDouble(5));
				a.setEstado(rta.getString(6));
				Collection <ItemMineral> componentes = ItemMineral.getInstancia().buscarItems_lote(a.getNro());
				a.setMineralesComponentes(componentes);
				dev.add(a);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return dev;
	}



	@Override
	public void update(Object o) {
		try
		{
			LoteSimple a = (LoteSimple)o;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("update  Lote set nro_mineralPrincipal = ?, area_siembra = ? where codigo = ?" );
			
			//agregar campos
			s.setLong(1, a.getMineralPrincipal().getCodigo());
			s.setDouble(2, a.getAreaSiembra());
			s.setLong(3, a.getNro());
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		}
		catch (Exception e)
		{
			System.out.println();
		}
		
	}

}
